<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/5
 * Time: 9:27
 */

namespace app\applet\model;


use think\Db;
use think\Exception;

class StoreApply
{
    /**
     *编辑商品
     */
    public function updateShop($data)
    {
        $where['sa_id'] = $data['sa_id'];
        unset($data['sa_id']);
        $apply['ic_id'] = $data['ic_id'];
        $apply['phone'] = $data['phone'];
        $apply['create_time'] = time();
        $detail['distribution_method'] = $data['distribution_method'];
        $detail['delivery_type'] = $data['delivery_type'];
        $detail['delivery_fee'] = $data['delivery_fee'];
        $detail['img_list'] = $data['img_list'];
        $detail['video_path'] = $data['video_path'];
//        $detail['advertisement'] = $data['advertisement'];
        $detail['settle_ratio'] = $data['settle_ratio'];

        Db::startTrans();
        try{
            Db::name("store_apply")
                ->where($where)
                ->update($apply);
            Db::name("store_detail")
                ->where($where)
                ->update($detail);
            // 提交事务
            Db::commit();
            return true;
        }catch (Exception $e)
        {
            Db::rollback();
            return false;
        }
    }
    public function ownAdvertisement()
    {
        $member_id = get_header_id("token");
        $store = Db::name("store_apply")
            ->field("sa_id")
            ->where(['member_id'=>$member_id])
            ->find();
        $img_list_data = Db::name("store_detail")
            ->field("own_advert")
            ->where(['sa_id'=>$store['sa_id']])
            ->find();

        $json['img'] = $img_list_data['own_advert'];
        return $json;
    }
    public function listAdvertisement()
    {
        $member_id = get_header_id("token");
        $store = Db::name("store_apply")
            ->field("sa_id")
            ->where(['member_id'=>$member_id])
            ->find();
        $advertisement = Db::name("store_detail")
            ->field("advertisement")
            ->where(['sa_id'=>$store['sa_id']])
            ->find();
        if($advertisement == NULL)
        {
            return [];
        }
        $advertisement = json_decode($advertisement['advertisement'],true);
        foreach ($advertisement as $k=>$v)
        {
            $info = Db::name("store_apply")
                ->field("company_name,phone")
                ->where(['sa_id'=>$v['store_id']])
                ->find();
            $advertisement[$k]['company_name'] = $info['company_name'];
            $advertisement[$k]['phone'] = $info['phone'];
        }
        return $advertisement;


    }
    public function addAdvertisement($advertisement)
    {
        $member_id = get_header_id("token");
        $store = Db::name("store_apply")
            ->field("sa_id")
            ->where(['member_id'=>$member_id])
            ->find();
        Db::name("store_detail")
            ->where(['sa_id'=>$store['sa_id']])
            ->update(["advertisement"=>$advertisement]);
    }
    public function shopAdvert($id)
    {
        $where['sa_id'] = $id;
        $data = Db::name("store_detail")
            ->field("advertisement")
            ->where($where)
            ->find();
        if($data['advertisement'] == null)
        {
            $json = [];
            return $json;
        }
        $json = json_decode($data['advertisement'],true);
        return $json;

    }
    public function ownAdvertisementAdd($img)
    {
        $member_id = get_header_id("token");
        $store = Db::name("store_apply")
            ->field("sa_id")
            ->where(['member_id'=>$member_id])
            ->find();
        $where['sa_id'] = $store['sa_id'];
        $data['own_advert'] = $img;
        Db::name("store_detail")->where($where)->update($data);

    }
    /**
     * 判断商家是否存在
     */
    public function whetherShop($phone)
    {
        $data = Db::name("store_apply")
            ->field("sa_id as store_id,company_name,phone")
            ->where(['phone'=>$phone])
            ->find();
        $ad = Db::name("store_detail")
            ->field("own_advert")
            ->where(['sa_id'=>$data['store_id']])
            ->find();
        $data['img'] = $ad['own_advert'];
        if($data === NULL){
            returnJson(-2004,"商家不存在");
        }
        return $data;
    }
    /**
     *查看店铺详情
     */
    public function shopDetail($member_id)
    {
        $data = Db::name("store_apply s")
            ->field("s.sa_id,s.company_name,s.ic_id,s.phone,s.address,s.longitude,s.latitude,s.address_detail,d.distribution_method,d.delivery_type,d.delivery_fee,d.settle_ratio,d.img_list,d.video_path,c.ic_name")
            ->alias(['mm_store_detail'=>'d','mm_industry_class'=>'c'])
            ->join("mm_store_detail","d.sa_id = s.sa_id")
            ->join("mm_industry_class","s.ic_id = c.ic_id",'left')
            ->where(['member_id'=>$member_id])
            ->find();
        $data['img_list'] = json_decode($data['img_list'],true);
        $data['video_path'] = json_decode($data['video_path'],true);

        return $data;
    }
    /**
     * 商铺行业分类下拉列表显示
     */
    public function industryClass()
    {
        $data = Db::name("industry_class")
            ->field("ic_id,ic_name,pic_domain,pic")
            ->order(['ic_sort'=>'desc'])
            ->select();
        return $data;
    }
    /**
     *商铺唯一性判断
     * @param $id int 商铺创建人的member_id
     */
    public function isOnly($id)
    {
        $count = Db::name("store_apply")
            ->where(['member_id'=>$id])
            ->count();
        if($count)
        {
            returnJson(-2004,'商铺已申请');
        }
    }
    /**
     * 保存商铺数据
     * @param $data  array  商铺基本数据
     */
    public function openShop($data)
    {
        Db::startTrans();
        try{
            $sa_id = Db::name("store_apply")
                ->insertGetId($data);
            Db::name("store_detail")
                ->insert(['sa_id'=>$sa_id]);
            // 提交事务
            Db::commit();
            return true;
        }catch (Exception $e)
        {
            Db::rollback();
            return false;
        }
    }

}